<?php
namespace app\admin\controller;
use think\Db;
use app\common\controller\order\Orderget;
class Adminuser extends Common
{
	//    用户列表页面
    public function index()
    {
		$Orderget=New Orderget();
    	$data=$Orderget->get_role();
		$this->assign('data',$data);
		return $this->fetch();	
    }
	//	 用户列表页面数据
    public function get_list()
    {	
		$page = input('page');
		$limit = input('limit');
		$TrueName= input('TrueName');//真实姓名
		$UserName= input('UserName');//用户名
		$RoleID=input('RoleID');//角色ID
		$field = input('post.');
	    $where=[];
		if($TrueName){
			$where['TrueName'] = $TrueName;
		}
		if($UserName){
			$where['UserName'] = $UserName;
		}
		if($RoleID){
			$where['RoleID'] = $RoleID;
		}
		$data=Db::table('xb_sys_administrator')->page($page,$limit)->order('ID desc')->where($where)->select();
		$cuont=Db::table('xb_sys_administrator')->where($where)->count('ID');	
        $UserID=session('id');
	    $RoleID=Db::table('xb_sys_administrator')->where(['ID'=>$UserID])->value('RoleID');
		$num='0';
		if($RoleID!='1'){
			foreach ($data as $k => $v) {
				if($v['RoleID']=='1'){
					unset($data[$k]);
					$num=$num+1;
					
				}
			}
		}	
		foreach ($data as $k => $v) {
			if($v['Status']=='0'){
				$v['Status']='<span style="color:red;">禁用</span>';
			}
			if($v['Status']=='1'){
					$v['Status']='<span style="color:green;">启用</span>';

			}
			if($v['isMAC']=='0'){
				$v['isMAC']='<span style="color:green;">不开启</span>';
			}
			if($v['isMAC']=='1'){
					$v['isMAC']='<span style="color:red;">开启</span>';
			}
			$v['RoleID']=Db::table('xb_sys_role')->where(array('ID'=> $v['RoleID']))->value('Name');
			$data[$k]=$v;
		}
		songsong($data,$cuont-$num);	
		
    }
	
	
	//	添加管理员  页面
	public function adminform(){

		$Orderget=New Orderget();
    	$data=$Orderget->get_role();
		$this->assign('data',$data);
		return $this->fetch();	
	}
	
	//	添加管理员 操作
	public function add()
    {
		$this->logs('添加管理员');

		$postData = input('post.');
        //检测添加角色是否为贷超 如果是添加控制字段 否则删除参数并正常添加管理员
		if(isset($postData['RoleID']) && $postData['RoleID'] == 48 && isset($postData['TemplateField'])){
            $postData['TemplateField'] = implode(',',$postData['TemplateField']);
        }else{
		    if(isset($postData['TemplateField'])){
                unset($postData['TemplateField']);
            }
        }

		//登陆方式 1手机 2密码
        if(isset($postData['LoginType']) && !empty($postData['LoginType']) && $postData['LoginType'] == 'on'){
            $postData['LoginType'] = 1;
        }else{
            $postData['LoginType'] = 2;
        }

        $postData['Password'] = md5('123456');
        $postData['CreateTime'] = date('Y-m-d H:i:s');//审核时间
		$ID = Db::table('xb_sys_administrator')->insert($postData);
		if($ID){
	        show_success();
		}else{
			show_error();
		}
	}
	//	删除管理员
	public function del(){
		$this->logs('删除管理员');
		$ID= input('ID');
		if($ID=='1' || $ID=='2'){
			show_error('','此账户禁止删除');
		}
	    $re=Db::table('xb_sys_administrator')->where('ID',$ID)->delete();
		if($re){
	    show_success();
		}else{
			show_error();
		}	
	}
//	编辑管理员页面
	public function editss(){
		$ID= input('ID');
		$Orderget=New Orderget();
    	$data=$Orderget->get_role();
		$data1=Db::table('xb_sys_administrator')->where('ID',$ID)->find();
        $data1['TemplateField'] = explode(',',$data1['TemplateField']);

		$this->assign('data',$data);
		$this->assign('data1',$data1);
		return $this->fetch();
	}
	//	编辑管理员操作
	public function edit(){
		$this->logs('编辑管理员');
		$data= input();
		if(!empty($data['isMAC']) && $data['isMAC']=='on'){
			$data['isMAC']=1;
		}else{
			$data['isMAC']=0;
		}
		if(!empty($data['Status']) && $data['Status']=='on'){
			$data['Status']=1;
		}else{
			$data['Status']=0;
		}

		//显示的统计字段
		if(isset($data['TemplateField']) && !empty($data['TemplateField'])){
            $data['TemplateField'] = implode(',',$data['TemplateField']);
        }

        //登陆方式 1手机 2密码
        if(isset($data['LoginType']) && !empty($data['LoginType']) && $data['LoginType'] == 'on'){
            $data['LoginType'] = 1;
        }else{
            $data['LoginType'] = 2;
        }

        //md5加密密码
        if(isset($data['Password']) && !empty($data['Password'])){
            $data['Password'] = md5($data['Password']);
        }else{
            if(isset($data['Password'])){
                unset($data['Password']);
            }
        }

        //贷超角色才添加控制显示字段 不是贷超角色设置空字段
        if(isset($data['RoleID']) && $data['RoleID'] != 48){
            $data['TemplateField'] = '';
        }

		$data['UpdateTime']=date('Y-m-d H:i:s');//时间
		$re=Db::table('xb_sys_administrator')->update($data);
		if($re){
	        show_success();
		}else{
			show_error();
		}
	}
}
